//
//  AWRefreshFooder.m
//  awu
//
//  Created by centling on 15/9/1.
//  Copyright (c) 2015年 ifly. All rights reserved.
//

#import "AWRefreshFooder.h"

@implementation AWRefreshFooder

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）

-(UIImageView *)loadingView{
    if (!_loadingView) {
        _loadingView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresh_up"]];
        _loadingView.frame=CGRectMake(SCREEN_WIDTH/2-15, 5, 30, 30);
    }
    return _loadingView;
}

-(UILabel *)label{
    if (!_label) {
        _label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        _label.textAlignment=NSTextAlignmentCenter;
        _label.font=[UIFont systemFontOfSize:13.0f];
        _label.text=@"暂无更多";
        _label.textColor=[UIColor lightGrayColor];
    }
    return _label;
}

-(CABasicAnimation *)rotationAnimation{
    if (!_rotationAnimation) {
        _rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        _rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        _rotationAnimation.duration = 1.5;
        _rotationAnimation.cumulative = YES;
        _rotationAnimation.repeatCount = MAXFLOAT;
    }
    return _rotationAnimation;
}

- (void)prepare
{
    [super prepare];
    [self addSubview:self.loadingView];
    [self addSubview:self.label];
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    self.loadingView.frame=CGRectMake(SCREEN_WIDTH/2-15, 5, 30, 30);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            self.label.hidden=YES;
            self.loadingView.hidden=NO;
            [self.loadingView.layer removeAllAnimations];
            break;
        case MJRefreshStatePulling:
            self.label.hidden=YES;
            self.loadingView.hidden=NO;
            [self.loadingView.layer removeAllAnimations];
            break;
        case MJRefreshStateRefreshing:
            self.label.hidden=YES;
            self.loadingView.hidden=NO;
            [self.loadingView.layer addAnimation:self.rotationAnimation forKey:@"rotationAnimation"];
            break;
        case MJRefreshStateNoMoreData:{
            [self.loadingView.layer removeAllAnimations];
            self.loadingView.hidden=YES;
            self.label.hidden=NO;
        }
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
}
@end
